#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''form database export soma infomation'''

import MySQLdb
import os
import matplotlib.pyplot as plt


def writeFile(filename, tupleList, path=r'C:\Users\fwk\Desktop\data'):
    '''write the data form database into txt,actually
    the data is tuple of tuple,use list to declare apparently'''
    try:
        if not os.path.exists(path):
            os.mkdir(path)
        f = file(os.path.join(path, filename), 'w')
        f.write(tupleList[0])
        f.write(tupleList[1])
        for line in tupleList[2]:
            f.write(
                str(line) +
                '\n-----------------------------------------------------\n\n')
    finally:
        f.close()


def getData(tableName):
    '''get result from table'''
    try:
        conn = MySQLdb.connect('10.131.252.156', 'root', 'root', 'fdroid')
        if conn:
            print 'connect success'

        sql_gitcommit = 'select commit_id,repository_id,commit_date,additions,\
        deletions,committer from gitcommit where repository_id = "2601" order by commit_date'

        sql_change_line = 'select commit_id,repository_id,file_name,line_number,content,\
        type from change_line where repository_id = "2601"'

        sql_change_operation = 'select commit_id,repository_id,file_name,change_type,\
        significance_level from change_operation where repository_id = "2601"'

        sqldict = {
            'gitcommit': sql_gitcommit,
            'change_line': sql_change_line,
            'change_operation': sql_change_operation
        }

        try:
            cursor = conn.cursor()
            n = cursor.execute(sqldict[tableName])
            firstLine = str(n) + ' records for: ' + tableName + '\n\n'
            secondLine = (sqldict[tableName])[7:(sqldict[tableName]).find(
                'from')].strip() + '\n++++++++++++++++++++++++++++++++++++\n'

            #even return,the finally block will be execute.

        finally:
            cursor.close()
            conn.close()

    except Exception:
        pass


def getcc(repository_id):
    '''get data from table like cc_*'''
    print 'getcc running'
    conn = MySQLdb.connect('10.131.252.156', 'root', 'root', 'fdroid')

    cursor = conn.cursor()
    cursor.execute(
        'SELECT * FROM fdroid.cc_clonepair where repository_id = %s and detection_id = 1'
        % repository_id)
    writeFile('clonepair.txt', ('', '', cursor.fetchall()))

    cursor.execute(
        'SELECT * FROM fdroid.cc_block where repository_id = %s and detection_id = 1'
        % repository_id)
    writeFile('block.txt', ('', '', cursor.fetchall()))

    cursor.execute(
        'SELECT * FROM fdroid.cc_file where repository_id = %s and detection_id = 1'
        % repository_id)
    writeFile('file.txt', ('', '', cursor.fetchall()))

    cursor.execute(
        'SELECT * FROM fdroid.cc_release where repository_id = %s and detection_id = 1'
        % repository_id)
    writeFile('release.txt', ('', '', cursor.fetchall()))

    cursor.execute(
        'SELECT * FROM fdroid.cc_repository where repository_id = %s and detection_id = 1'
        % repository_id)
    writeFile('repository.txt', ('', '', cursor.fetchall()))

    cursor.close()
    conn.close()
    print 'ok'


def getgit(repository_id):
    '''get data from table gitrelease,gitcommit and change_line'''
    print 'getgit running'
    conn = MySQLdb.connect('10.131.252.156', 'root', 'root', 'fdroid')

    cursor = conn.cursor()
    cursor.execute('SELECT * FROM fdroid.gitcommit where repository_id = %s' %
                   repository_id)
    writeFile('gitcommit.txt', ('', '', cursor.fetchall()))

    cursor.execute('SELECT * FROM fdroid.gitrelease where repository_id = %s' %
                   repository_id)
    writeFile('gitrelease.txt', ('', '', cursor.fetchall()))

    cursor.execute('SELECT * FROM fdroid.change_line where repository_id = %s'
                   % repository_id)
    writeFile('change_line.txt', ('', '', cursor.fetchall()))

    cursor.close()
    conn.close()
    print 'ok'


def get_change_line(repository_id):
    '''every commit info will be separate to an new file like change_line_0,change_line_1...'''
    print 'get_change_line running'
    conn = MySQLdb.connect('10.131.252.156', 'root', 'root', 'fdroid')

    cursor = conn.cursor()
    cursor.execute(
        'SELECT * FROM fdroid.change_line where repository_id = %s order by commit_id'
        % repository_id)

    commit_id = 0
    path = r'C:\Users\fwk\Desktop\data'
    filename = 'change_line_0.txt'
    f = file(os.path.join(path, filename), 'w')
    flag = cursor.fetchone()[1]
    cursor.scroll(0, 'absolute')
    for line in cursor.fetchall():
        if line[1] == flag:
            f.write(
                str(line) +
                '\n-----------------------------------------------------\n\n')
        else:
            print str(commit_id) + ' was completed'
            commit_id += 1
            f.close()
            flag = line[1]
            filename = filename[:12] + str(commit_id) + '.txt'
            f = file(os.path.join(path, filename), 'w')
            f.write(
                str(line) +
                '\n-----------------------------------------------------\n\n')

    f.close()
    cursor.close()
    conn.close()
    print 'ok'


def showplt():
    '''compare the number of commit in gitcommit and change_line'''
    conn = MySQLdb.connect('10.131.252.156', 'root', 'root', 'fdroid')
    cursor1 = conn.cursor()
    cursor2 = conn.cursor()
    path = r'C:\Users\fwk\Desktop\data\statistics.txt'
    f = file(path, 'w')
    x = []
    y1 = []
    y2 = []
    #for i in range(737, 3074):
    for i in range(1000, 1200):
        cursor1.execute(
            'SELECT count(distinct commit_id) FROM fdroid.gitcommit where repository_id =%s'
            % i)
        cursor2.execute(
            'SELECT count(distinct commit_id) FROM fdroid.change_line where repository_id =%s'
            % i)
        n1 = cursor1.fetchone()[0]
        n2 = cursor2.fetchone()[0]

        if n2 > 20 and n1:

            if (abs(n1 - n2) < 25):
                print i
                x.append(i)
                y1.append(n1)
                y2.append(n2)
            if n1 == n2:
                f.write(str(i) + ',' + str(n1) + ',' + str(n2) + '\n')

    f.close()
    cursor1.close()
    cursor2.close()
    conn.close()

    plt.plot(x, y1, label='gitcommit', linewidth=2, color='r', marker='o', markerfacecolor='blue', markersize=5)
    plt.plot(x, y2, label='change_line', marker='o', markerfacecolor='red', markersize=5)
    plt.xlabel('repository_id')
    plt.ylabel('number of commit')
    plt.title('Convert Graph\nCheck it out')
    plt.legend()
    plt.show()


def get_clonepairNumber_of_eachRelease(filepath = r'C:\Users\fwk\Desktop\dataof1709\clonepair.txt'):
    f = file(filepath)
    
    clonepair = [0, ]
    release_id = f.readline().split(',')[2]
    print release_id == " 0L"
    f.seek(0)
    for line in f.readlines():
        if line.startswith('('):
            print line.split(',')[2]
            if release_id == line.split(',')[2]:
                
                clonepair[int(release_id[:-1])] += 1
            else:
                release_id = line.split(',')[2]
                clonepair.append(1)

    f.close()
    print clonepair
    x = range(0,len(clonepair))
    plt.plot(x, clonepair, label='repository_id =1709', linewidth=2, color='r', marker='o', markerfacecolor='blue', markersize=5)
    
    plt.xlabel('release_id')
    plt.ylabel('number of clonepair')
    plt.title('Convert Graph\nCheck it out')
    plt.legend()
    plt.annotate('Rapid growth', xy=(18, 32), xytext=(20, 32),\
        arrowprops = dict(facecolor = "r", headlength = 0.5, headwidth = 0.5, width = 0.3))
    
    plt.xticks(range(len(clonepair)))
    plt.show()


if __name__ == '__main__':

    #getData('gitcommit')
    #getData('change_line')
    #getData('change_operation')
    get_clonepairNumber_of_eachRelease()
    #get_clonepairNumber_of_eachRelease(r'C:\Users\fwk\Desktop\dataof2078\clonepair.txt')
    #showplt()
    #getcc(94)
    #getgit(1006)
    